#include "rpc/net/buffer.h"
#include "rpc/net/connection.h"
#include "rpc/net/protocol.h"
#include "rpc/net/processor.h"
#include "include/rpc_helper.h"

namespace rpc {

bool Protocol::HandleRead(Connection* conn, Buffer* buf) {
  CHECK(conn != NULL && buf != NULL);
  uint32 readn;
  const RpcHeader* bhs;

  switch (conn->stat()) {
    case Connection::ST_FREE:
      conn->SetStat(Connection::ST_HEADER);
      readn = (uint32) conn->Recv(HEADER_LENGTH);
      if (readn != HEADER_LENGTH) {
        return false;
      }

    case Connection::ST_HEADER:
      conn->SetStat(Connection::ST_DATA);
      bhs = (RpcHeader*) buf->Data();
      readn = conn->Recv(bhs->length);
      if (readn != bhs->length) {
        return false;
      }

    case Connection::ST_DATA:
      conn->SetStat(Connection::ST_FREE);
      break;
  }

  return true;
}

void Protocol::HandleWrite(Connection* trans, uint32 rpc_id, uint32 func_id,
                           const Message* msg) {
}

}

